package com.jnc.core.web.exception;

import com.jnc.common.base.BaseResp;
import com.jnc.common.constant.SysConstant;
import com.jnc.common.exception.BaseException;
import com.jnc.common.exception.DemoException;
import com.jnc.common.util.convert.StringUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Auther: jjn
 * @Date: 2018/11/10
 * @Description: 异常统一处理类
 */
@RestControllerAdvice
public class DefaultExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    /**
     * 系统业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public BaseResp handleBaseException(BaseException e){
        logger.error("System business error, Exception: {}", e.getMessage());
        BaseResp result = new BaseResp();
        return result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg(e.getMessage());
    }

    /**
     * 权限校验失败
     * @param e
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public BaseResp handleAuthorizationException(AuthorizationException e){
        logger.error("Permission author failed, Exception: {}", ExceptionUtils.getStackTrace(e));
        String permission = StringUtil.substringBetween(e.getMessage(), "[", "]");
        BaseResp result = new BaseResp();
        return result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("用户没有权限[" + permission + "]");
    }

    /**
     * 请求方式不支持
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public BaseResp handleRequestNotSupportException(HttpRequestMethodNotSupportedException e){
        logger.error("Http request method is not support, Exception: {}", ExceptionUtils.getStackTrace(e));
        BaseResp result = new BaseResp();
        return result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("不支持[" + e.getMethod() + "]请求");
    }

    /**
     * 拦截未知运行时异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResp handleUnknownException(RuntimeException e){
        logger.error("Run time error, Exception: {}", ExceptionUtils.getStackTrace(e));
        BaseResp result = new BaseResp();
        return result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("运行时异常：" + e.getMessage());
    }

    /**
     * 系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public BaseResp handleException(Exception e){
        logger.error("Server error, Exception: {}", ExceptionUtils.getStackTrace(e));
        BaseResp result = new BaseResp();
        return result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("服务器错误，请联系管理员");
    }

    /**
     * 演示模式异常
     * @param e
     * @return
     */
    @ExceptionHandler(DemoException.class)
    public BaseResp handleDemoException(DemoException e){
        logger.error("Demo mode error, Exception: {}", e);
        BaseResp result = new BaseResp();
        return result.setCode(SysConstant.STATUS_CODE_ERROR).setMsg("演示模式，不允许操作");
    }
}
